package com.ts.ischool.dao;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.ts.ischool.pojo.CalendarBean;
import com.ts.ischool.utils.DateUtils;

public class CalendarDaoImpl extends SqlMapClientDaoSupport implements CalendarDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<CalendarBean> queryCalendars(Date qstart, Date qend,
			String userId) {
		Map<String,String> map = new HashMap<String,String>();
		map.put("startTime", DateUtils.getFormateDateStr(qstart));
		map.put("endTime", DateUtils.getFormateDateStr(qend));
		map.put("userId", userId);
		
		return (List<CalendarBean>) this.getSqlMapClientTemplate().queryForList("calendar.queryCalendars", map);
	}

	@Override
	public Long addCalendar(CalendarBean c) {
		Long id = (Long) this.getSqlMapClientTemplate().insert("calendar.addCalendar",c);
		return id;
	}

	@Override
	public void addCalendarBatch(List<CalendarBean> list) throws SQLException {
		try {
			this.getSqlMapClient().startTransaction();
			this.getSqlMapClient().startBatch();
			for (CalendarBean c : list) {
				this.getSqlMapClient().insert("calendar.addCalendar", c);
			}
			this.getSqlMapClient().executeBatch();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.getSqlMapClient().endTransaction();
		}
	}

	@Override
	public CalendarBean getCalendar(String id) {
		Map<String,String> map = new HashMap<String,String>();
		map.put("calendarId", id);
		Object obj = null;
		obj = this.getSqlMapClientTemplate().queryForObject("calendar.getCalendar", map);
		if (obj == null)
			return null;
		else
			return (CalendarBean) obj;
	}

	@Override
	public void updateCalendar(CalendarBean c) {
		this.getSqlMapClientTemplate().update("calendar.updateCalendar", c);
	}

	@Override
	public void deleteCalendar(String id) {
		Map<String,String> map = new HashMap<String,String>();
		map.put("calendarId", id);
		this.getSqlMapClientTemplate().delete("calendar.deleteCalendar", map);

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CalendarBean> findCalendarsByIds(String[] ids) {
		Map<String,String[]> map = new HashMap<String,String[]>();
		map.put("ids", ids);
		return (List<CalendarBean>) this.getSqlMapClientTemplate().queryForList("calendar.findCalendarsByIds", map);
	}

	@Override
	public void updateCalendarBatch(List<CalendarBean> list)
			throws SQLException {
		try {
			this.getSqlMapClient().startTransaction();
			this.getSqlMapClient().startBatch();
			for (CalendarBean c : list) {
				this.getSqlMapClient().update("calendar.updateCalendar", c);
			}
			this.getSqlMapClient().executeBatch();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.getSqlMapClient().endTransaction();
		}
	}

	@Override
	public void deleteCalendarByGoogleId(String id) {
		Map<String,String> map = new HashMap<String,String>();
		map.put("googleId", id);
		this.getSqlMapClientTemplate().delete("calendar.deleteCalendarByGoogleId", map);
	}

	@Override
	public CalendarBean getCalendarByGoogleId(String id) {
		Map<String,String> map = new HashMap<String,String>();
		map.put("googleId", id);
		Object obj = null;
		obj = this.getSqlMapClientTemplate().queryForObject("calendar.getCalendarByGoogleId", map);
		if (obj == null)
			return null;
		else
			return (CalendarBean) obj;
	}

	@Override
	public void deleteCalendarBatch(String[] ids) {
		Map<String,String[]> map = new HashMap<String,String[]>();
		map.put("ids", ids);
		this.getSqlMapClientTemplate().delete("calendar.deleteCalendarBatch", map);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CalendarBean> findCalendarsByUserId(Long id) {
		Map<String,Long> map = new HashMap<String,Long>();
		map.put("userId", id);
		return (List<CalendarBean>) this.getSqlMapClientTemplate().queryForList("calendar.findCalendarsByUserId", map);
	}

}
